<template>
  <div>
    <slot></slot>
    <el-input
      :placeholder="$t('m.inputPhone')"
      v-model="mobile"
      :class="color == 'black' ? 'black_input' : 'white_input'"
      @change="mobileChange"
    >
      <el-select
        v-model="countryCode"
        popper-class="leftselect"
        filterable
        slot="prepend"
        :popper-append-to-body="false"
        @change="codeChange"
      >
        <el-option
          v-for="item in cities"
          :key="item.en"
          :label="'+' + item.code"
          :value="'+' + item.code"
        >
          <span>{{ item[lang] + "+" + item.code }}</span>
        </el-option>
      </el-select>
    </el-input>
  </div>
</template>

<script>
import list from "@/assets/josn/code.json";
import vPinyin from "@/assets/js/hanziToPinyin.js";
export default {
  props: {
    color: String,
    ObjectName: String,
    phoneNum: String,
    code: String,
  },
  data() {
    return {
      lang: "",
      codes: [], //区列表
      cities: [],
      mobile: "",
      countryCode: "+86",
    };
  },
  mounted() {
    this.lang = localStorage.getItem("lang");
    this.$nextTick(function () {
      list.forEach((element) => {
        element.lang = this.lang;
      });
      list.sort(function (a, b) {
        var x, y;
        x = vPinyin.chineseToPinYin(a[a.lang]).toLowerCase();
        y = vPinyin.chineseToPinYin(b[a.lang]).toLowerCase();
        if (x < y) {
          return -1;
        }
        if (x > y) {
          return 1;
        }
        return 0;
      });
      this.cities = list;
    });
  },
  watch: {
    phoneNum(val) {
      if (val != "") this.mobile = val;
    },
    code(val) {
      if (val != "") this.countryCode = val;
    },
  },
  methods: {
    mobileChange(val) {
      this.$emit("mobileChange", val, this.ObjectName);
    },
    codeChange(val) {
      this.$emit("codeChange", val, this.ObjectName);
    },
  },
};
</script>

<style lang="less">
.black_input {
    display: flex;
    border-radius: 3px;
    border: 1px solid @hoverColor;
    .el-input__inner {
      color: @fff;
      background: transparent;
      border: none;
      font-size: 12px;
      border-radius: 3px;
    }
    .el-input-group__prepend {
      background: transparent;
      border: none;
      padding: 0;
      width: 66px;
      &::after {
        content: "|";
        color: @fbSix;
        width: 1px;
        height: 14px;
        line-height: 14px;
      }
      .el-select {
        width: 66px;
        position: relative;
        margin: 0;
        border-radius: 3px;
        height: 38px;
        color: @fff;
        .el-input--suffix .el-input__inner {
          padding-right: 20px;
          padding-left: 10px;
          border-radius: 3px;
        }
        .el-popper.leftselect {
          color: @fff;
          background-color: @LightBorderLine;
          border: 1px solid @fbSix;
          li {
            border-bottom: none;
          }
          .el-select-dropdown__item {
            color: @fff;
          }
          .el-select-dropdown__item.selected {
            color: @hoverColor;
          }
          .el-select-dropdown__item.hover,
          .el-select-dropdown__item:hover {
            background-color: @deepBg;
            color:@hoverColor;
            font-weight: 700;
          }
        }
      }
    }
    .el-input__inner:-moz-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner::-moz-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner:-ms-input-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner::-webkit-input-placeholder {
      color: @fbSix;
    }
}
.white_input {
    display: flex;
    border-radius: 3px;
    border: 1px solid @fbSix;
    .el-input__inner {
      color: @fbSix;
      background: transparent;
      border: none;
      font-size: 12px;
      border-radius: 3px;
    }
    .el-input-group__prepend {
      background: transparent;
      color: @fbSix;
      border: none;
      padding: 0;
      width: 66px;
      &::after {
        content: "|";
        color: @fbSix;
        width: 1px;
        height: 14px;
        line-height: 14px;
      }
      .el-select {
        width: 66px;
        position: relative;
        margin: 0;
        border-radius: 3px;
        height: 38px;
        .el-input--suffix .el-input__inner {
          padding-right: 20px;
          padding-left: 10px;
          border-radius: 3px;
        }
        .el-popper.leftselect {
          color: #606266;
          background-color: @fff;
          border: 1px solid @fff;
          li {
            border-bottom: none;
          }
          .el-select-dropdown__item {
            color: #606266;
          }
          li:hover {
            background: @fff;
            color: #409eff;
            font-weight: 700;
          }
          .el-select-dropdown__item.selected {
            color: #409eff;
          }
          .el-select-dropdown__item.hover,
          .el-select-dropdown__item:hover {
            background-color: @fff;
            color: #409eff;
            font-weight: 700;
          }
        }
      }
    }
    .el-input__inner {
      background: transparent;
      border: none;
      font-size: 12px;
      border-radius: 3px;
    }
    .el-input__inner:-moz-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner::-moz-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner:-ms-input-placeholder {
      color: @fbSix;
    }
  
    .el-input__inner::-webkit-input-placeholder {
      color: @fbSix;
    }
}
.el-input__suffix {
  cursor: pointer;
  .el-input__inner {
    padding-left: 17px;
    padding-right: 0;
  }
}
input::-webkit-input-placeholder {
  color: @fbSix;
}
input::-moz-input-placeholder {
  color: @fbSix;
}
input::-ms-input-placeholder {
  color: @fbSix;
}
</style>